package me.ingxin.android.tools.app.drawer

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import me.ingxin.android.tools.app.databinding.ItemPageHorizontalBinding
import me.ingxin.android.tools.app.databinding.ItemPageTxtBinding

/**
 * Created on 2022/05/26
 */
class PageTxtAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    /**修改成自己的数据类型*/
    val datas = ArrayList<String>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == 0) {
            return ViewHolder(
                ItemPageTxtBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        } else {
            return HorizontalViewHolder(
                ItemPageHorizontalBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        }


    }

    override fun getItemViewType(position: Int): Int {
        return if (position == 2) {
            1
        } else {
            0
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is ViewHolder -> holder.bind(datas[position])
            is HorizontalViewHolder -> holder.bind(datas[position])
        }
    }

    override fun getItemCount(): Int = datas.size

    class ViewHolder(private val binding: ItemPageTxtBinding) :
        RecyclerView.ViewHolder(binding.root) {

        private lateinit var item: String

        init {
            binding.tv.setOnClickListener {
                Toast.makeText(it.context, "点击$item", Toast.LENGTH_SHORT).show()
            }
        }


        fun bind(item: String) {
            this.item = item
            binding.tv.text = item
        }
    }

    class HorizontalViewHolder(private val binding: ItemPageHorizontalBinding) :
        RecyclerView.ViewHolder(binding.root) {

        private lateinit var item: String

        init {
            binding.rv.layoutManager =
                LinearLayoutManager(itemView.context, LinearLayoutManager.HORIZONTAL, false)
            binding.rv.adapter = PageHorizontalAdapter()
        }


        fun bind(item: String) {
            this.item = item
        }
    }


}